package com.benz.order.util.multithread;

public class DeadLockThread implements Runnable {
    static final Object chopsticks = new Object();
    static final Object knifeAndFork = new Object();
    private final boolean flag;

    DeadLockThread(boolean flag) {
        this.flag = flag;
    }
    @Override
    public void run() {
        if(flag){
            while(true){
                synchronized (chopsticks){//chopsticks上的同步代码块
                    System.out.println(Thread.currentThread().getName() + "---if---chopsticks");
                    //knifeAndFork上的同步代码块
                    synchronized (knifeAndFork){
                        System.out.println(Thread.currentThread().getName() + "---if---knifeAndFork");
                    }
                }
            }
        }else{
            while(true){
                synchronized (knifeAndFork){//knifeAndFork 上的同步代码块
                    System.out.println(Thread.currentThread().getName() + "---else---knifeAndFork");
                    //chopsticks 上的同步代码块
                    synchronized (chopsticks){
                        System.out.println(Thread.currentThread().getName() + "---else---chopsticks");
                    }
                }
            }
        }
    }
}
